<?php


namespace app\api\service;


use think\Db;

class OrderService
{
    /**
     * 根据订单id查询订单详情
     * @param $orderId
     * @return mixed
     */
    public function fetchOrderDetail($orderId){

        $sql = "select credit, billno, id as order_id from jn_wx_order_info where id = ?";

        $orderDetail = Db::query($sql, [$orderId]);

        return $orderDetail;
    }


    /**
     * 获取接单超时的药店
     * @return mixed
     */
    public function fetchTimeoutToAcceptOrder(){

        $sql = "select order_id, pharmacy_id from jn_pharmacy_order where TIMESTAMPDIFF(second, created_at, NOW() ) >= 3600 and status = 1 GROUP BY pharmacy_id limit 100 ";

        $orderDetail = Db::query($sql);

        return $orderDetail;

    }

    /**
     * 获取已支付待派单的订单
     * @return mixed
     */
    public function fetchHasPayOrder(){

        $sql ="select id, latitude, longitude, addtime, recipients, recipientsMobile  from jn_wx_order_info where o_status = 2 and pharmacy = 1";

        $orders = Db::query($sql);

        return $orders;


    }


    /**
     * 更新订单的状态
     *
     * @param $orderId
     * @param $status
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function updateOrderStatus($orderId, $status){

        $resultCount = Db::name('wx_order_info')
            ->where('id', $orderId)
            ->where('o_status', 2)
            ->update(['o_status' => $status]);
        if($resultCount > 0){
            return true;
        }else{
            return false;
        }

    }

    /**
     * 获取正在派送的订单
     * @return mixed
     */
    public function fetchSendingOrder(){

        $sql = "select id, order_id, pharmacy_id, status, settlement_amount from jn_pharmacy_order where status = 230 limit 100";

        $data = Db::query($sql);

        return $data;

    }
}